//
//  YXGeneralViewController.m
//  MockProject
//
//  Created by FanYu on 25/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXGeneralViewController.h"
#import "YXAccessibilityTableViewCell.h"
#import "YXDownloadNumTableViewCell.h"
#import "YXNavigationController.h"
#import "YXPickerView.h"
#import "YXUserDefault.h"
#import "YXNavigationController.h"


@interface YXGeneralViewController () <UITableViewDelegate, UITableViewDataSource, YXPickerViewDelegate, YXAccessibilityTableViewCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) YXPickerView *pickerView;
@property (nonatomic, copy) NSString *selectedRow;

@end

@implementation YXGeneralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"通用设置";
    
    [self setupUI];
    [self configData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    YXNavigationController *navi = (YXNavigationController *)self.navigationController;
    [navi changeToWhiteBar];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    YXNavigationController *navi = (YXNavigationController *)self.navigationController;
    [navi changeToDefaultBar];
}


#pragma mark - Setup UI
- (void)setupUI {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    self.tableView.rowHeight = 50;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.tableView.separatorColor = [UIColor colorWithHexString:@"e0dcd9"];
    [self.tableView registerNib:[UINib nibWithNibName:@"YXDownloadNumTableViewCell" bundle:nil] forCellReuseIdentifier:@"YXDownloadNumTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"YXAccessibilityTableViewCell" bundle:nil] forCellReuseIdentifier:@"YXAccessibilityTableViewCell"];
    [self.view addSubview:self.tableView];
}


#pragma mark - Config Data
- (void)configData {
    YXSettingCellModel *accessibilityModel = [YXSettingCellModel configWithTitle:@"仅WiFi环境下载" icon:@"" number:@"" isOn:[YXUserDefault onlyDownloadOnWiFi]];
    YXSettingCellModel *maxDownloadNumModel = [YXSettingCellModel configWithTitle:@"最大同时下载数量" icon:@"" number:[YXUserDefault maxSimultaneouslyNumber] isOn:[YXUserDefault onlyDownloadOnWiFi]];
    
    self.dataArray = [[NSMutableArray alloc] init];
    [self.dataArray addObject:accessibilityModel];
    [self.dataArray addObject:maxDownloadNumModel];
    
    [self.pickerView.pickerView selectRow:[YXUserDefault maxSimultaneouslyNumber].integerValue - 1 inComponent:0 animated:NO];
    self.selectedRow = [YXUserDefault maxSimultaneouslyNumber];
}


#pragma mark - Picker View Animation 
- (void)showPickerView {
    self.maskView.alpha = 0;
    [self.navigationController.view addSubview:self.maskView];
    
    self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 245);
    [self.navigationController.view addSubview:self.pickerView];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height - 245, self.view.bounds.size.width, 245);
        self.maskView.alpha = 0.2;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hidePickerView {
    [UIView animateWithDuration:0.2 animations:^{
        self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 245);
        self.maskView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.pickerView removeFromSuperview];
        [self.maskView removeFromSuperview];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YXAccessibilityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXAccessibilityTableViewCell" forIndexPath:indexPath];
        cell.cellModel = self.dataArray[indexPath.section];
        cell.delegate = self;
        
        return cell;
        
    } else {
        YXDownloadNumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXDownloadNumTableViewCell" forIndexPath:indexPath];
        cell.cellModel = self.dataArray[indexPath.section];
        return cell;
    }
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        [self showPickerView];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor colorWithHexString:@"f5f5f5"];
}


#pragma mark - Actions 
- (void)didTapMaskView:(UITapGestureRecognizer *)sender {
    [self hidePickerView];
}

#pragma mark - Alert
- (void)showAlert:(NSString *)title message:(NSString *)msg {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self.navigationController presentViewController:alertController animated:YES completion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alertController dismissViewControllerAnimated:YES completion:nil];
            [alertController removeFromParentViewController];
        });
    }];
}

#pragma mark - YXPickerViewDelegate
- (void)cancelButtonTapped:(UIButton *)sender {
    [self hidePickerView];
}

- (void)saveButtonTapped:(UIButton *)sender {
    [self hidePickerView];
    
    [YXUserDefault setMaxSimultaneouslyNumber:self.selectedRow];
    
    YXSettingCellModel *maxDownloadNumModel = [YXSettingCellModel configWithTitle:@"最大同时下载数量" icon:@"" number:self.selectedRow isOn:NO];
    
    [self.dataArray replaceObjectAtIndex:1 withObject:maxDownloadNumModel];
    
    [self.tableView reloadData];
    
    [self showAlert:@"保存成功" message:nil];
}

- (void)didSelectRow:(NSInteger)row {
    self.selectedRow = [NSString stringWithFormat:@"%ld", row];
}



#pragma mark - YXAccessibilityTableViewCellDelegate
- (void)switchTapped:(UISwitch *)sender {
    [YXUserDefault setOnlyDownloadOnWiFi:sender.isOn];
    
    YXSettingCellModel *accessibilityModel = [YXSettingCellModel configWithTitle:@"仅WiFi环境下载" icon:@"" number:@"" isOn:[YXUserDefault onlyDownloadOnWiFi]];
    [self.dataArray replaceObjectAtIndex:0 withObject:accessibilityModel];
}



#pragma mark - Lazy Init 
- (YXPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[YXPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 245)];
        _pickerView.delegate = self;
    }
    return _pickerView;
}

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:self.view.frame];
        self.maskView.backgroundColor = [UIColor blackColor];
        [_maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMaskView:)]];
    }
    return _maskView;
}

@end
